from django.db import models
from apps.Index.models import Users
from tinymce.models import HTMLField


class Movies(models.Model):
    movieId = models.IntegerField(db_column='movieId', primary_key=True,
                                  verbose_name='影片编号')
    title = models.CharField(max_length=80, verbose_name='影片名称')
    genres = models.CharField(max_length=100, verbose_name='影片体裁')
    movieDesc = HTMLField(verbose_name='电影简介')
    movieYear = models.CharField(max_length=10, verbose_name='影片年份')

    class Meta:
        managed = False
        db_table = 'movies'
        verbose_name = '电影信息'

    def getJsonObj(self):
        movie = {
            "movieId": self.movieId,
            "title": self.title,
            'genres': self.genres,
            'movieDesc': self.movieDesc,
            'movieYear': self.movieYear
        }
        return movie


class Ratings(models.Model):
    ratingId = models.IntegerField(db_column='ratingId', primary_key=True,
                                  verbose_name='评分编号')
    userId = models.OneToOneField(Users, models.DO_NOTHING, db_column='userId',
                                  verbose_name='用户编号')
    movieId = models.ForeignKey(Movies, models.DO_NOTHING, db_column='movieId',
                                verbose_name='电影编号')
    rating = models.FloatField(verbose_name='用户评分')
    timestamp = models.CharField(max_length=11, verbose_name='时间戳')

    class Meta:
        managed = False
        db_table = 'ratings'
        unique_together = (('ratingId',),)


class Tags(models.Model):
    userId = models.OneToOneField(Users, models.DO_NOTHING, db_column='userId',
                                  primary_key=True, verbose_name='用户编号')
    movieId = models.ForeignKey(Movies, models.DO_NOTHING, db_column='movieId', verbose_name='电影编号')
    tagId = models.IntegerField(db_column='tagId', verbose_name='标签编号')
    tag = models.CharField(max_length=100, verbose_name='标签')
    timestamp = models.CharField(max_length=10, verbose_name='时间戳')

    class Meta:
        managed = False
        db_table = 'tags'
        unique_together = (('userId', 'movieId', 'tagId'),)


class Relevancy(models.Model):
    movieId = models.OneToOneField(Movies, models.DO_NOTHING, db_column='movieId',
                                   primary_key=True, verbose_name='电影编号')
    tagId = models.IntegerField(db_column='tagId', verbose_name='标签编号')
    relevance = models.FloatField(verbose_name='关系度')

    class Meta:
        managed = False
        db_table = 'relevancy'
        unique_together = (('movieId', 'tagId'),)


class MovieGenre(models.Model):
    movieGenresId = models.AutoField(primary_key=True, verbose_name='影片体裁编号')
    movieGenresName = models.CharField(max_length=40, default='', verbose_name='影片体裁名称')

    class Meta:
        db_table = 'genre'
        verbose_name = '电影体裁'
        verbose_name_plural = verbose_name
